package com.igroupes.ovineherd.model.attribute.date;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.DateRangeStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.model.utils.ListUtils;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.igroupes.ovineherd.util.DateUtils;
import com.igroupes.ovineherd.util.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 日期
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class DateRangeModelData extends DateBasicAttribute{

    public DateRangeModelData() {
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        DateRangeModelData dateBasicAttribute = (DateRangeModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("maxDate", Convert.toStr(dateBasicAttribute.getMaxDate().getValue()));
        jsonObject.set("minDate", Convert.toStr(dateBasicAttribute.getMinDate().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        dataModelAttributeEntity.setDbNumber(2);
        fieldQuery.setLength(10);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);

//        FieldQuery fieldQuery1 = new FieldQuery();
//        BeanUtil.copyProperties(fieldQuery, fieldQuery1);
//        fieldQuery1.setFieldName(fieldQuery.getFieldName()+"1");
//        dataModelAttributeEntity.setDbName(fieldQuery.getFieldName()+"1");
//        fieldQuery1.setDataModelAttributeEntity(null);
//        fieldQueries.add(fieldQuery1);
        return fieldQueries;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);
        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        JSONObject jsonObject = modelAttributeEntity.getJson();
        String minDate = jsonObject.get("minDate").toString();
        String maxDate = jsonObject.get("maxDate").toString();
//        if(!(value instanceof ArrayList)){
//            throw new ModelException("传值格式错误！");
//        }

        if(ObjectUtil.isNotNull(value)){
            String str = Convert.toStr(value);
            if(str.contains(",")){
                String[] dateStyleValue =str.split(",");
                if(StringUtils.isNotBlank(dateStyleValue[0])) {
                    if (!DateUtils.checkDate(dateStyleValue[0], DateUtils.DATE_FORMAT)) {
                        throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
                    }
                    if(StringUtils.isNotBlank(minDate)) {
                        if (!DateUtils.timeVerification(minDate, dateStyleValue[0], DateUtils.DATE_FORMAT)) {
                            throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能小于" + minDate);
                        }
                    }
                    if(StringUtils.isNotBlank(maxDate)) {
                        if (!DateUtils.timeVerification(dateStyleValue[0], maxDate, DateUtils.DATE_FORMAT)) {
                            throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能大于" + maxDate);
                        }
                    }
                }
                if(StringUtils.isNotBlank(dateStyleValue[1])) {
                    if (!DateUtils.checkDate(dateStyleValue[1], DateUtils.DATE_FORMAT)) {
                        throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
                    }
                    if(StringUtils.isNotBlank(minDate)) {
                        if (!DateUtils.timeVerification(minDate, dateStyleValue[1], DateUtils.DATE_FORMAT)) {
                            throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能小于" + minDate);
                        }
                    }
                    if(StringUtils.isNotBlank(maxDate)) {
                        if (!DateUtils.timeVerification(dateStyleValue[1], maxDate, DateUtils.DATE_FORMAT)) {
                            throw new ModelException(modelAttributeEntity.getName() + "传值错误，时间不能大于" + maxDate);
                        }
                    }
                }
                if(StringUtils.isNotBlank(dateStyleValue[0]) && StringUtils.isNotBlank(dateStyleValue[1])) {

                    if(!DateUtils.timeVerification(dateStyleValue[0],dateStyleValue[1], DateUtils.DATE_FORMAT)){
                        throw new ModelException(jsonObject.get("leftName")+"不能大于"+jsonObject.get("rightName"));
                    }
                }
                FieldApiResponse fieldApiResponse = new FieldApiResponse();
                fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
                fieldApiResponse.setValue(ArrayUtil.join(dateStyleValue, ","));
                fieldApiResponse.setIsStr(true);
                fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
                fieldApiResponses.add(fieldApiResponse);
//                FieldApiResponse fieldApiResponse1 = new FieldApiResponse();
//                fieldApiResponse1.setDbName(modelAttributeEntity.getDbName()+"1");
//                fieldApiResponse1.setValue(dateStyleValue[1]);
//                fieldApiResponse1.setIsStr(true);
//                fieldApiResponse1.setType(MySqlTypeKey.VARCHAR.getCode());
//                fieldApiResponses.add(fieldApiResponse1);
            }

        }
        return fieldApiResponses;
    }

    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        String minDate = jsonObject.get("minDate").toString();
        String maxDate = jsonObject.get("maxDate").toString();
        return new JSONObject(new DateRangeStyle(
                dataModelAttributeEntity.getName(),new ArrayList<>(),
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
                ,minDate, maxDate
        ), false);
    }
    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.DATE.getCode(), true, true, true);
    }

    @Override
    public Object getData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        if(ObjectUtil.isNotNull(value)){
            return value.toString().split(",");
        }
        return value;
    }

}
